/*
 * Copyright 2008 Carnegie Mellon University
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, 
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

function initTM() {
  loadHeaderSub( lang );
  loadTaskList();
  loadMessages(lang);
  document.title = 'SEPIA: '+msg( lang, 'msg_tm' );
}

function loadTaskList() {
  var list = $("taskList");
  
  try {
    var param = eval('$H({"action":"GetTasks" })').toQueryString();
    var json = sync(param);
    initSelect(list);
    if (json.tasks.length>0) {
      jQuery('#select_a_task').slideDown("slow",footer);
    }
    for (var i = 0; i < json.tasks.length; i++) {
      var opt = document.createElement("option");
      opt.innerHTML = json.tasks[i].taskNameShort;
      opt.value = json.tasks[i].taskNameShort;
      //opt.id = "task"+json.tasks[i].taskId;
      list.appendChild(opt);
    }
    // no task is defined
    if ( json.tasks.length==0 ) { 
      $('taskList').style.width = '160px';
      $('taskList').disable();
    }
  } catch (err) {
    var opt = document.createElement("option");
    opt.innerHTML = "&nbsp;&nbsp;&nbsp;&nbsp;*No Task has been created yet*&nbsp;&nbsp;&nbsp;&nbsp;";
    opt.selected = true;
    list.appendChild(opt);
    opt.disabled = true;
  }
}

function loadTask( taskNameShort ) {
  if ( taskNameShort=="" ) {
    jQuery('#additional').slideUp("slow",footer);
  } else {
    jQuery('#additional').slideUp("fast", function(){
      var param = eval('$H({"action":"GetTask", "taskNameShort": "'+taskNameShort+'"})').toQueryString();
      var json = sync(param);
      loadTaskIntoForm(json);
      jQuery('#additional').slideDown("slow",footer);
    });
  }
}

function addTask() {
  jQuery('#additional').slideUp("fast", function(){
    var newTaskNameShort = $('newTask').value;
    var param = [];
    param.action        = "AddTask";
    param.taskId        = -1;
    param.taskNameShort = es( newTaskNameShort );
    
    var json = sync(param);
    loadTaskIntoForm(json);
    
    var t = json.tasks[0];
      
    // update topicList 
    var opt = document.createElement("option");
    opt.value = newTaskNameShort;
    opt.innerHTML = newTaskNameShort;
    opt.id = "taskList"+t.taskId;
    
    $('taskList').appendChild( opt );
    $(opt.id).selected = true;
    $('newTask').value = '';
    
    showMessage( msg_suc_updated );
    log("ADD TASK",newTaskNameShort);
    jQuery('#additional').slideDown("slow",footer);
    jQuery('#select_a_task').slideDown("slow",footer);
  });
  
  return true;
}

function deleteTask() {
  if ( !deleteConfirmed() ) return;

  var param = eval('$H({"action":"DeleteTask", "taskNameShort": "'+$('taskNameShort').value+'" })').toQueryString();
  sync(param);
  var json = eval("({'tasks':[{'taskId': -1, 'taskName': '', 'taskNameShort': '', 'description': '', 'isMonolingual': '0', 'srcLang': '', 'trgLang': '1', 'types': '', 'segmenterCmd': '', 'indriPath': '', 'currentPhase': '0' }] })");
  //$('additional').style.display = 'none';
  jQuery('#additional').slideUp("slow");
  loadTaskIntoForm(json);
  loadTaskList();
  log("DELETE TASK",$('taskName').value);
  showMessage( msg_suc_updated );
}

function saveTask() {
  var param = [];
  param.action       = "UpdateTask"; 
  param.taskId       = $('taskId').value; 
  param.taskName     = es( $('taskName').value ); 
  param.taskNameShort= es( $('taskNameShort').value ); 
  param.description  = es( $('description').value ); 
  param.isMonolingual= ($('isMonolingual').checked?1:0); 
  param.srcLang      = es( $('srcLang').value ); 
  param.trgLang      = es( $('trgLang').value ); 
  param.types        = es( $('types').value ); 
  param.tdLabels     = es( $('tdLabels').value ); 
  param.irLabels     = es( $('irLabels').value ); 
  param.segmenterCmd = ''; 
  param.indriPath    = es( $('indriPath').value ); 
  param.currentPhase = es( $('currentPhase').value ); 
  sync(param);
  loadTaskList();
  log("UPDATE TASK",$('taskName').value);
  
  $('taskList').enable();
  
  jQuery("#taskList option[value='"+$('taskNameShort').value+"']").selected(true);
//  var opts = $('taskList').options;
//  for ( var i=0; i<opts.length; i++) {
//    if( opts[i].value==$('taskNameShort').value ){
//    opts[i].selected = true;
//    }
//  }
  showMessage( msg_suc_updated );
}

function loadTaskIntoForm(json) {
  var t = json.tasks[0];
  $('newTask').value = '';
  
  $('taskId').value = t.taskId;
  $('taskName').value = ues( t.taskName );
  $('taskNameShort').value = ues( t.taskNameShort );
  $('description').value = ues( t.description );
  $('isMonolingual').checked = ( t.isMonolingual==1 )?true:false;
  $('srcLang').value = ues( t.srcLang );
  $('trgLang').value = ues( t.trgLang );
  $('types').value = ues( t.types );
  $('tdLabels').value = ues( t.tdLabels );
  $('irLabels').value = ues( t.irLabels );
//  $('segmenterCmd').value = ues( t.segmenterCmd );
  $('indriPath').value = ues( t.indriPath );
  $('currentPhase').value = t.currentPhase;

  $('srcLangContainer').style.display=$('isMonolingual').checked ? "none":"";
  
  jQuery("#srcLangList option[value='"+$('srcLang').value+"']").selected(true);
  jQuery("#trgLangList option[value='"+$('trgLang').value+"']").selected(true);
}
